package cn.topevery.garden.dal.logic;

import cn.topevery.garden.dal.dao.EvaluationFileDao;
import cn.topevery.garden.dal.dao.EvaluationLogDao;
import cn.topevery.garden.entity.po.EvaluationLog;
import cn.topevery.garden.entity.query.EvaluationLogQuery;
import cn.topevery.garden.entity.query.EvaluationRulesQuery;
import cn.topevery.garden.entity.vo.EvaluationLogVO;
import cn.topevery.garden.framework.base.BaseLogic;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 检查考评上报记录表
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Service
public class EvaluationLogLogic extends BaseLogic<EvaluationLog> {
    @Autowired
    EvaluationLogDao evaluationLogDao;

    @Autowired
    EvaluationFileDao evaluationFileService;

    @Override
    public BaseEntityDao<EvaluationLog, Integer> getDao() {
        return evaluationLogDao;
    }

    

    /**
     * 获取列表
     *
     * @param query
     */
    public DataPager<EvaluationLogVO> listPage(EvaluationLogQuery query) {
        return evaluationLogDao.listPage(query);
    }

    /**
     * 根据自增id批量删除
     *
     * @param ids
     */
    public void deleteBatchById(String userId,Integer[] ids) {
        evaluationLogDao.delete(userId, ids);
    }

    /**
     * 根据自增id保存或者更新
     *
     * @param evaluationLog 实体
     */
    public void saveOrUpdate(String userId, EvaluationLog evaluationLog){
        evaluationLogDao.saveOrUpdate(userId,evaluationLog);
    }

    /**
         * 根据自定义列值来获取对象
         */
    public EvaluationLog getEntityByColumn(String key, String value) {
        return evaluationLogDao.getEntityByColumn(key, value);
    }

    public void commit(EvaluationLog log, Integer userId){
        log.setDbUpdateDate(LocalDateTime.now());
        log.setDbUpdateId(userId);
        log.setUserId(userId);
        if (log.getClassScore() - log.getCounts() * log.getRuleScore() > 0 || log.getClassScore() == 0) {
            log.setDockScore(log.getCounts() * log.getRuleScore());
        } else {
            log.setDockScore(log.getClassScore());
        }
        if (log.getId() != null) {//修改
            evaluationFileService.deleteByLogId(log.getId(), userId);
            evaluationLogDao.update(userId.toString(),log);
        } else {
            evaluationLogDao.insert(userId.toString(),log);
        }
        if (log.getFileList() != null && !log.getFileList().isEmpty()) {
            evaluationFileService.saveFiles(log.getFileList().stream().peek(temp -> {
                temp.setLogId(log.getId());
                temp.setDbStatus(false);
                temp.setDbUpdateDate(LocalDateTime.now());
                temp.setDbUpdateId(userId);
            }).collect(Collectors.toList()));
        }
    }

    public List<EvaluationLog> getReportList(EvaluationRulesQuery param){
        return  evaluationLogDao.getReportList(param);
    }

    public void deleteById(Integer id, Integer userId){
          evaluationLogDao.deleteById(id,userId);
    }

    public List<Map<String,Object>> getAllLogForMap(){
        return  evaluationLogDao.getAllLogForMap();
    }
}
